package com.cosc716.application.gui.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ListPanel extends JPanel {
	
	private static final long serialVersionUID = 6248687622719051726L;
	
	private String listTitle = "";
	private DefaultListModel listModel = new DefaultListModel();
	private MultiLineCellRenderer cellRenderer = new MultiLineCellRenderer();
	private JList list = new JList(listModel);
	private JScrollPane scrollList = new JScrollPane(list);
	
	public ListPanel(String listTitle) {
		
		listTitle = listTitle.trim();
		if(!listTitle.endsWith(":")) {
			listTitle += ":";
		}
		
		this.listTitle = listTitle;
		
		list.setCellRenderer(cellRenderer);
		
		this.setLayout(new GridBagLayout());

		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.gridx = gbc.gridy = 0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		this.add(new JLabel(this.listTitle), gbc);
		
		gbc.gridy = 1;
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = gbc.weighty = 1.0;
		this.add(scrollList, gbc);
	}
	
	public void clearList() {
		listModel.clear();
	}
	
	public void addListItem(String listItem) {
		listModel.addElement(listItem);
	}
}